package com.yct.springcloud.controller;

import com.yct.springcloud.entity.pay.Payment;
import com.yct.springcloud.support.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.awt.*;

/**
 * 订单Controller
 */
@RestController
@Slf4j
public class OrderController {

    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Resource
    private RestTemplate restTemplate;

    @PostMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {


        log.info("Consumer：插入支付【" + payment + "】");

        return restTemplate.postForObject(PAYMENT_URL + "/yct/payment/insertPayment", payment, CommonResult.class);  //写操作
    }

    @GetMapping(value = "/consumer/payment/get/{id}"/*,produces = { "application/json;charset=UTF-8" }*/)
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id) {

        log.info("Consumer：根据id【" + id + "】查询支付数据。");
        //restTemplate.getForObject 返回对象为响应体中数据转化成的对象，基本可以理解为JSON
        return restTemplate.getForObject(PAYMENT_URL + "/yct/payment/get/" + id, CommonResult.class);
    }
    @GetMapping(value = "/consumer/payment/getForEntity/{id}"/*,produces = { "application/json;charset=UTF-8" }*/)
    public CommonResult<Payment> getPayment1(@PathVariable("id") Long id) {

        log.info("Consumer：根据id【" + id + "】查询支付数据。");

        //restTemplate.getForEntity 返回对象为 ResponseEntity 对象，包含了响应中的一些重要信息，比如响应头、响应状态码、响应体等...
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_URL + "/yct/payment/get/" + id, CommonResult.class);

        if (entity.getStatusCode().is2xxSuccessful()){
            //成功状态
            return entity.getBody();
        }else {
            return new CommonResult<>(501,"操作失败");
        }

//        return restTemplate.getForObject(PAYMENT_URL + "/yct/payment/get/" + id, CommonResult.class);
    }
}

